package com.hllxd.hism.passport.login.gateway.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;

/**
 * Personal Computer PC端登录对应的 @Controller
 * @author lixiyong01
 * @date 2021.11.07
 */
@Controller
@Slf4j
public class PcLoginRouteController {

    private static final Map<String, String> PAGE_ROUTE = new HashMap<String, String>();
    static {
        PAGE_ROUTE.put("localhost", "index");
        // mis系统的域名 templates/index2.html
        PAGE_ROUTE.put("rock.58corp.net", "index2");
        //支付的域名 templates/t_pay.html
        PAGE_ROUTE.put("local.58corp.com", "index");
    }

    private String routeLoginPage(String redirectUrl) {
        String page = null;
        for (Map.Entry<String, String> entry : PAGE_ROUTE.entrySet()) {
            if (redirectUrl.contains(entry.getKey())) {
                page = entry.getValue();
                break;
            }
        }

        return page;
    }
    @RequestMapping(value = "/") public String index() {
        return "index";
    }
    @RequestMapping(value = "/user_add") public String addUser() {
        return "user";
    }
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String logIn(@RequestParam(value = "redirectURL", required = false) String redirectURL) throws Exception {
        log.info("login ....");
        if (StringUtils.isEmpty(redirectURL)) {
            // 统一的登录页面里面应该提供供用户跳转的系统的首页
            return "index";
        }
        // 判断是哪条线过来的跳转登录请求，显示各个系统的登录页面，如果没有对应的话那么就显示统一的登录页面
        String loginPage = routeLoginPage(redirectURL);
        log.info("loginPage: {}", loginPage);
        if (StringUtils.isEmpty(loginPage)) {
            return "index";
        } else {
            return loginPage;
        }
    }
}
